const {
  $util,
} = getApp();
let isPrevImg = false;
const MAX_LENGTH = 10;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    idx: '',
    page: 1,
    list: [],
    hasMore: true,
    showInput: false,
    content: '',
  },
  isprevent: false,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(options);
    this.getList();
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    })
    if (isPrevImg) {
      isPrevImg = false;
    }
  },
  getList() {
    $util.request({
      url: 'entry/wxapp/group.comment.commentshow',
      data: {
        cid: this.data.id,
        page: this.data.page,
      },
      showLoading: false,
      success: (res) => {
        let list = res.data.plist;
        if (this.data.page > 1) {
          list = [...this.data.list, ...list];
        }
        this.setData({
          list,
          info: res.data,
          hasMore: !(this.data.page > 1 && res.data.plist.length !== MAX_LENGTH),
          loaded: true,
        });
      },
    });
  },
  showInput(e) {
    this.setData({
      showInput: !this.data.showInput,
      typ: e.currentTarget.dataset.typ || '',
      cid: e.currentTarget.dataset.id || '',
      commentid: e.currentTarget.dataset.cid || '',
    });
  },
  bindIpval(e) {
    this.setData({
      content: e.detail.value,
    });
  },
  bindlinechange(e) {
    console.log(e);
    this.setData({
      heightRpx: e.detail.heightRpx,
    });
  },
  sendComment() {
    if (!this.data.content) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
      });
      return;
    }
    if (this.isprevent) return;
    this.isprevent = true;
    $util.request({
      url: 'entry/wxapp/group.comment.addcomment',
      data: {
        topicid: this.data.info.topicid,
        content: this.data.content,
        type: this.data.typ, // 1发帖 2评论 3追加评论
        pid: this.data.cid,
        commentid: this.data.commentid,
      },
      showLoading: false,
      success: () => {
        this.setData({
          page: 1,
          hasMore: true,
          typ: '',
          cid: '',
          content: '',
          showInput: false,
        });
        this.getList();
      },
      complete: () => {
        this.isprevent = false;
      },
    });
  },
  // 删除评论
  deteleComment(e) {
    const { list, info } = this.data;
    const { idx, id, isrelist } = e.currentTarget.dataset;
    let cid = '';
    wx.showModal({
      title: '提示',
      content: '确认要删除该评论吗？',
      success: (res) => {
        if (res.confirm) {
          $util.request({
            url: 'entry/wxapp/group.comment.myrecorddelete',
            data: {
              id,
            },
            success: () => {
              if (Number(isrelist) !== 0) {
                for (const i in list) {
                  if (Number(isrelist) === Number(list[i].id)) {
                    cid = i;
                    break;
                  }
                }
                list[cid].clist.splice(idx, 1);
              } else {
                list.splice(idx, 1);
                info.commentnum = --info.commentnum;
              }
              this.setData({
                list,
                info,
              });
            },
          });
        }
      },
    });
  },
  // 帖子点赞
  bindlike(e) {
    const { id } = e.currentTarget.dataset;
    $util.request({
      url: 'entry/wxapp/group.comment.setlike',
      data: {
        commentid: id,
      },
      showLoading: false,
      success: (res) => {
        const { info } = this.data;
        info.islike = Number(res.code);
        info.likenum = Number(res.allnum);
        this.setData({
          info,
        });
      },
    });
  },
  // 评论点赞
  bindcommentlike(e) {
    const { id, idx } = e.currentTarget.dataset;
    $util.request({
      url: 'entry/wxapp/group.comment.setlike',
      data: {
        pid: id,
      },
      showLoading: false,
      success: (res) => {
        const { list } = this.data;
        list[idx].islike = Number(res.code);
        list[idx].likenum = Number(res.allnum);
        this.setData({
          list,
        });
      },
    });
  },
  onPrevImg() {
    isPrevImg = true;
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.hasMore && this.getList();
  },
});
